package org.hmh.leetcode.hashtable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

public class code599_easy {
    class Solution {
        public String[] findRestaurant(String[] list1, String[] list2) {
            List<String> result = new ArrayList<>();
            HashMap<String, Integer> set = new HashMap<>();
            for (int i = 0; i < list1.length; i++) {
                set.put(list1[i], i);
            }
            int min = Integer.MAX_VALUE;
            for (int i = 0; i < list2.length; i++) {
                if (set.containsKey(list2[i])) {
                    int sum = i + set.get(list2[i]);
                    if (sum < min) {
                        min = sum;
                        result.clear();
                        result.add(list2[i]);
                    } else if (sum == min) {
                        result.add(list2[i]);
                    }
                }
            }
            return result.toArray(new String[0]);
        }
    }

}
